<?php

namespace iBrand\Shop\Backend\Repository;

use iBrand\Component\Goods\Models\SpecsValue;

class O2oGoodsProductsRepository extends \iBrand\Shop\Core\Repositories\O2oGoodsProductsRepository
{
    public function getGoodsProductsPaginate($where, array $ids = [], $with = [], $limit = 20, $baseGoodsWhere = [])
    {
        $query = $this->scopeQuery(function ($query) use ($where, $ids, $with, $baseGoodsWhere) {
            if (is_array($where) AND count($where) > 0) {
                foreach ($where as $key => $value) {
                    if ($key == 'goods') {
                        continue;
                    }

                    if (is_array($value)) {
                        list($operate, $va) = $value;
                        $query = $query->where($key, $operate, $va);
                    } else {
                        $query = $query->where($key, $value);
                    }
                }
            }

            if (!empty($ids)) {
                $query = $query->whereIn('goods_id', $ids);
            }

            if (!empty($where['goods']['product_ids'])) {
                $query = $query->whereIn('product_id', $where['goods']['product_ids']);
            }

            if (!empty($with)) {
                foreach ($with as $item) {
                    $query = $query->with($item);
                }
            }

            return $query->whereHas('baseGoods', function ($query) use ($baseGoodsWhere) {
                foreach ($baseGoodsWhere as $key => $value) {
                    if (is_array($value)) {
                        list($operate, $va) = $value;
                        $query = $query->where($key, $operate, $va);
                    } else {
                        $query = $query->where($key, $value);
                    }
                }
            })->orderBy('id', 'desc');
        });

        if ($limit) {
            return $query->paginate($limit);
        } else {
            return $query->all();
        }
    }

    public function getExcelGoods($where, array $ids = [], $with = [], $limit = 20, $baseGoodsWhere = [])
    {
        $query = $this->scopeQuery(function ($query) use ($where, $ids, $with, $baseGoodsWhere) {
            if (is_array($where) AND count($where) > 0) {
                foreach ($where as $key => $value) {
                    if ($key == 'goods') {
                        continue;
                    }

                    if (is_array($value)) {
                        list($operate, $va) = $value;
                        $query = $query->where($key, $operate, $va);
                    } else {
                        $query = $query->where($key, $value);
                    }
                }
            }

            if (!empty($ids)) {
                $query = $query->whereIn('goods_id', $ids);
            }

            if (!empty($where['goods']['product_ids'])) {
                $query = $query->whereIn('product_id', $where['goods']['product_ids']);
            }

            if (!empty($with)) {
                foreach ($with as $item) {
                    $query = $query->with($item);
                }
            }

            return $query->whereHas('baseGoods', function ($query) use ($baseGoodsWhere) {
                foreach ($baseGoodsWhere as $key => $value) {
                    if (is_array($value)) {
                        list($operate, $va) = $value;
                        $query = $query->where($key, $operate, $va);
                    } else {
                        $query = $query->where($key, $value);
                    }
                }
            })->orderBy('id', 'desc');
        });

        $products = $query->paginate($limit);
        $lastPage = $products->lastPage();

        //'商品ID', 'SKU', '商品编号', '商品名称', '吊牌价', '采购价', '尺码', '颜色', '分类', '数量'
        $lists = [];
        if ($products->count()) {
            foreach ($products as $key => $item) {
                $lists[$key]['goods_id'] = $item->goods_id;
                $lists[$key]['sku'] = $item->baseProduct->sku;
                $lists[$key]['goodsSn'] = $item->baseGoods->goods_no;
                $lists[$key]['goodsName'] = $item->baseGoods->name;
                $lists[$key]['market_price'] = $item->baseGoods->market_price;
                $lists[$key]['factory_price'] = $item->baseGoods->factory_price;

                $tmpSpec = SpecsValue::find($item->baseProduct->specID[0]);
                if ($tmpSpec->spec_id == 1) {
                    $colorId = $item->baseProduct->specID[1];
                    $sizeId = $item->baseProduct->specID[0];
                } else {
                    $colorId = $item->baseProduct->specID[0];
                    $sizeId = $item->baseProduct->specID[1];
                }
                $color = SpecsValue::find($colorId);
                $size = SpecsValue::find($sizeId);
                $lists[$key]['color'] = $color->name;
                $lists[$key]['size'] = $size->name;

                $categories = $item->baseGoods->categories()->pluck('name')->toArray();
                $lists[$key]['categories'] = implode(',', $categories);

                $lists[$key]['quantity'] = $item->store_nums;
            }
        }
        return [
            'products' => $lists,
            'lastPage' => $lastPage,
        ];
    }
}